package leets.leet2342;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int maximumSum(int[] nums) {
        Map<Integer,int[]> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num=nums[i];
            int key=getSum(num);
            if (map.containsKey(key)){

                int[] ints=map.get(key);
                int p=ints[0]>ints[1]?1:0;
                ints[p]=Math.max(num,ints[p]);

            }else {
                map.put(key,new int[]{num,0});
            }
        }
        int max=-1;
        for (int[] a:map.values()){
            if(a[1]!=0){
                max=Math.max(a[0]+a[1],max);
            }
        }
        return max;
    }
    public int getSum(int num){
        int res=0;

        while (num>0){
            res+=(num%10);
            num/=10;
        }
        return res;
    }
}